home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 4 / Example 4.7 / app.cpp next >
Encoding:
C/C++ Source or Header  |  2006-08-01  |  7.3 KB  |  279 lines

  1. //////////////////////////////////////////////////////////////
  2. // Example 4.7: Texture Splatting                            //
  3. // Written by: C. Granberg, 2005                            //
  4. //////////////////////////////////////////////////////////////
  5.  
  6. #include <windows.h>
  7. #include <d3dx9.h>
  8. #include <vector>
  9. #include "debug.h"
  10. #include "heightMap.h"
  11. #include "terrain.h"
  12.  
  13. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  14. #define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  15.  
  16. class APPLICATION
  17. {
  18.     public:
  19.         
  20.         APPLICATION();
  21.         HRESULT Init(HINSTANCE hInstance, int width, int height, bool windowed);
  22.         HRESULT Update(float deltaTime);
  23.         HRESULT Render();
  24.         HRESULT Cleanup();
  25.         HRESULT Quit();
  26.  
  27.     private:
  28.         IDirect3DDevice9* m_pDevice; 
  29.         TERRAIN m_terrain;
  30.  
  31.         float m_angle, m_radius;
  32.         bool m_wireframe;
  33.         DWORD m_time;
  34.         int m_fps, m_lastFps;
  35.         HWND m_mainWindow;
  36.         ID3DXFont *m_pFont;
  37. };
  38.  
  39. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)
  40. {
  41.     APPLICATION app;
  42.  
  43.     if(FAILED(app.Init(hInstance, 800, 600, true)))
  44.         return 0;
  45.  
  46.     MSG msg;
  47.     memset(&msg, 0, sizeof(MSG));
  48.     int startTime = timeGetTime(); 
  49.  
  50.     while(msg.message != WM_QUIT)
  51.     {
  52.         if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
  53.         {
  54.             ::TranslateMessage(&msg);
  55.             ::DispatchMessage(&msg);
  56.         }
  57.         else
  58.         {    
  59.             int t = timeGetTime();
  60.             float deltaTime = (t - startTime)*0.001f;
  61.  
  62.             app.Update(deltaTime);
  63.             app.Render();
  64.  
  65.             startTime = t;
  66.         }
  67.     }
  68.  
  69.     app.Cleanup();
  70.  
  71.     return msg.wParam;
  72. }
  73.  
  74. APPLICATION::APPLICATION()
  75. {
  76.     m_pDevice = NULL; 
  77.     m_mainWindow = 0;
  78.     m_angle = 0.0f;
  79.     m_radius = 100.0f;
  80.     m_wireframe = false;
  81.     m_fps = m_lastFps = 0;
  82.     m_time = GetTickCount();
  83.  
  84.     srand(GetTickCount());
  85. }
  86.  
  87. HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed)
  88. {
  89.     debug.Print("Application initiated");
  90.  
  91.     //Create Window Class
  92.     WNDCLASS wc;
  93.     memset(&wc, 0, sizeof(WNDCLASS));
  94.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  95.     wc.lpfnWndProc   = (WNDPROC)::DefWindowProc; 
  96.     wc.hInstance     = hInstance;
  97.     wc.lpszClassName = "D3DWND";
  98.  
  99.     //Register Class and Create new Window
  100.     RegisterClass(&wc);
  101.     m_mainWindow = CreateWindow("D3DWND", "Example 4.7: Texture Splatting", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); 
  102.     SetCursor(NULL);
  103.     ShowWindow(m_mainWindow, SW_SHOW);
  104.     UpdateWindow(m_mainWindow);
  105.  
  106.     //Create IDirect3D9 Interface
  107.     IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  108.  
  109.     if(d3d9 == NULL)
  110.     {
  111.         debug.Print("Direct3DCreate9() - FAILED");
  112.         return E_FAIL;
  113.     }
  114.  
  115.     //Check that the Device supports what we need from it
  116.     D3DCAPS9 caps;
  117.     d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
  118.  
  119.     //Hardware Vertex Processing or not?
  120.     int vp = 0;
  121.     if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  122.         vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  123.     else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  124.  
  125.     //Check vertex & pixelshader versions
  126.     if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0))
  127.     {
  128.         debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0");
  129.     }
  130.  
  131.     //Set D3DPRESENT_PARAMETERS
  132.     D3DPRESENT_PARAMETERS d3dpp;
  133.     d3dpp.BackBufferWidth            = width;
  134.     d3dpp.BackBufferHeight           = height;
  135.     d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
  136.     d3dpp.BackBufferCount            = 1;
  137.     d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
  138.     d3dpp.MultiSampleQuality         = 0;
  139.     d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
  140.     d3dpp.hDeviceWindow              = m_mainWindow;
  141.     d3dpp.Windowed                   = windowed;
  142.     d3dpp.EnableAutoDepthStencil     = true; 
  143.     d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
  144.     d3dpp.Flags                      = 0;
  145.     d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  146.     d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;
  147.  
  148.     //Create the IDirect3DDevice9
  149.     if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow,
  150.                                  vp, &d3dpp, &m_pDevice)))
  151.     {
  152.         debug.Print("Failed to create IDirect3DDevice9");
  153.         return E_FAIL;
  154.     }
  155.  
  156.     //Release IDirect3D9 interface
  157.     d3d9->Release();
  158.  
  159.     D3DXCreateFont(m_pDevice, 18, 0, 0, 1, false,  
  160.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  161.                    DEFAULT_PITCH | FF_DONTCARE, "Arial", &m_pFont);
  162.  
  163.     //Create Terrain
  164.     m_terrain.Init(m_pDevice, INTPOINT(100,100));
  165.  
  166.     //Set sampler state
  167.     m_pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  168.     m_pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  169.     m_pDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
  170.  
  171.     return S_OK;
  172. }
  173.  
  174. HRESULT APPLICATION::Update(float deltaTime)
  175. {
  176.     //Control camera
  177.     m_angle += deltaTime * 0.5f;
  178.     D3DXMATRIX  matWorld, matView, matProj;        
  179.     D3DXVECTOR2 centre = D3DXVECTOR2(50, 50);
  180.     D3DXVECTOR3 Eye    = D3DXVECTOR3(centre.x + cos(m_angle) * m_radius, m_radius, -centre.y + sin(m_angle) * m_radius);
  181.     D3DXVECTOR3 Lookat = D3DXVECTOR3(centre.x, 0.0f,  -centre.y);
  182.  
  183.     D3DXMatrixIdentity(&matWorld);
  184.     D3DXMatrixLookAtLH(&matView, &Eye, &Lookat, &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  185.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.3333f, 1.0f, 1000.0f );
  186.  
  187.     m_pDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  188.     m_pDevice->SetTransform( D3DTS_VIEW,       &matView );
  189.     m_pDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  190.     
  191.     if(KEYDOWN('W'))
  192.     {
  193.         m_wireframe = !m_wireframe;
  194.         Sleep(300);
  195.     }
  196.     if(KEYDOWN(VK_SPACE))
  197.     {
  198.         m_terrain.GenerateRandomTerrain(3);
  199.     }
  200.     else if(KEYDOWN(VK_ADD) && m_radius < 200.0f)
  201.     {
  202.         m_radius += deltaTime * 30.0f;
  203.     }
  204.     else if(KEYDOWN(VK_SUBTRACT) && m_radius > 5.0f)
  205.     {
  206.         m_radius -= deltaTime * 30.0f;
  207.     }
  208.  
  209.     if(KEYDOWN(VK_ESCAPE))
  210.         Quit();
  211.  
  212.     return S_OK;
  213. }    
  214.  
  215. HRESULT APPLICATION::Render()
  216. {
  217.     // Clear the viewport
  218.     m_pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0L );
  219.  
  220.     //FPS Calculation
  221.     m_fps++;
  222.     if(GetTickCount() - m_time > 1000)
  223.     {
  224.         m_lastFps = m_fps;
  225.         m_fps = 0;
  226.         m_time = GetTickCount();
  227.     }
  228.  
  229.     // Begin the scene 
  230.     if(SUCCEEDED(m_pDevice->BeginScene()))
  231.     {
  232.         if(m_wireframe)m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);    
  233.         else m_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
  234.  
  235.         m_terrain.Render();
  236.  
  237.         RECT r[] = {{10, 10, 0, 0}, {10, 30, 0, 0}, {10, 50, 0, 0}};
  238.         m_pFont->DrawText(NULL, "W: Toggle Wireframe", -1, &r[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  239.         m_pFont->DrawText(NULL, "+/-: Zoom In/Out", -1, &r[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  240.         m_pFont->DrawText(NULL, "Space: Randomize Terrain", -1, &r[2], DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  241.  
  242.         //FPS
  243.         char number[50];
  244.         std::string text = "FPS: ";
  245.         text += _itoa(m_lastFps, number, 10);
  246.         RECT rc = {630, 10, 0, 0};
  247.         m_pFont->DrawText(NULL, text.c_str(), -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xff000000);
  248.  
  249.         // End the scene.
  250.         m_pDevice->EndScene();
  251.         m_pDevice->Present(0, 0, 0, 0);
  252.     }
  253.  
  254.     return S_OK;
  255. }
  256.  
  257. HRESULT APPLICATION::Cleanup()
  258. {
  259.     try
  260.     {
  261.         m_terrain.Release();
  262.  
  263.         m_pFont->Release();
  264.         m_pDevice->Release();
  265.  
  266.         debug.Print("Application terminated");
  267.     }
  268.     catch(...){}
  269.  
  270.     return S_OK;
  271.  
  272. }
  273.  
  274. HRESULT APPLICATION::Quit()
  275. {
  276.     ::DestroyWindow(m_mainWindow);
  277.     ::PostQuitMessage(0);
  278.     return S_OK;
  279. }